package org.jpush.utils;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.channels.AsynchronousServerSocketChannel;
import java.nio.channels.AsynchronousSocketChannel;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Utilities
{
	private static final SimpleDateFormat DEFAULT_FORMAT = new SimpleDateFormat("YYYY-mm-dd HH:MM:SS");
	public static String getCurrentTime()
	{
		return DEFAULT_FORMAT.format(new Date());
	}
	
	public static int getStringLength(String str)
	{
		return str != null ? str.length() : 0;
	}
	
	public static boolean isEmptyString(String str)
	{
		return (str == null || "".equals(str));
	}
	
	public static void StringCopy(StringBuffer sb, int offset, String str)
	{
        for (int idx = 0; idx < getStringLength(str); idx ++)
        {
            sb.setCharAt(offset + idx, str.charAt(idx));
        }
	}
	
	public static void charmemset(StringBuffer dest, int offset, char c, int l)
	{
	    //while ((l--) > 0) *(dest++)=c;
		for (int i = 0; i < l; i ++)
		{
			dest.setCharAt(offset + i, c);
		}
	}
	
	public static String xstrstr(String str1, String str2)
	{
		if (str1 == null || str2 == null)
		{
			return null;
		}
		else
		{
			int idx = str1.indexOf(str2);
			if (idx < 0)
			{
				return null;
			}
			else
			{
				return str1.substring(idx);
			}
		}
	}
	
	public static boolean objectEquals(Object obj1, Object obj2)
	{
		if (obj1 == null)
		{
			if (obj2 == null)
				return true;
			else
				return false;
		}
		else
		{
			return obj1.equals(obj2);
		}
	}
	
	public static void closeSocket(Socket s)
	{
		if (s != null)
		{
			try
			{
				s.close();
			} catch (IOException e) {}
		}
	}
	
	public static void shutdownSocket(Socket s, boolean shutdownInput, boolean shutdownOutput)
	{
		if (s != null)
		{
			if (shutdownInput)
			{
				try
				{
					s.shutdownInput();
				} catch (IOException e) {}
			}
			if (shutdownOutput)
			{
				try
				{
					s.shutdownOutput();
				} catch (IOException e) {}
			}
		}
	}
	
	public static void closeServerSocket(ServerSocket s)
	{
		if (s != null)
		{
			try
			{
				s.close();
			} catch (IOException e) {}
		}
	}
	
	public static void closeServerSocketChannel(ServerSocketChannel s)
	{
		if (s != null)
		{
			try
			{
				s.close();
			} catch (IOException e) {}
		}
	}
	
	public static void closeAsynchronousServerSocketChannel(AsynchronousServerSocketChannel s)
	{
		if (s != null)
		{
			try
			{
				s.close();
			} catch (IOException e) {}
		}
	}
	
	public static void closeSelector(Selector s)
	{
		if (s != null)
		{
			try
			{
				s.close();
			} catch (IOException e) {}
		}
	}
	
	public static void closeAsynchronousSocketChannel(AsynchronousSocketChannel socket)
	{
		if (socket != null)
		{
			try
			{
				socket.close();
			} catch (IOException e) {}
		}
	}
	
	public static void closeSocketChannel(SocketChannel socket)
	{
		if (socket != null)
		{
			try
			{
				socket.close();
			} catch (IOException e) {}
		}
	}
	
	public static int getProcessorsCount()
	{
	    return Runtime.getRuntime().availableProcessors();
	}
	
	public static String getPath(String path)
	{
		int pos1 = path.indexOf("\\");
		int pos2 = path.indexOf("/");
		int pos  = (pos1 == -1) ? pos2 : (pos2 == -1) ? pos1 : (Math.max(pos1, pos2));
		
		if (pos == -1)
			return path;
		else
			return path.substring(0, pos);
	}
}
